package com.cictec.middleware.gps.engine.position.handler.operateState;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.commons.utils.log.MultiLog;
import com.cictec.middleware.core.entity.BusScheduleApply;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionOperationHandler;
import com.cictec.middleware.gps.engine.utils.MultiLogUtil;
import com.cictec.middleware.gps.service.BusScheduleApplyService;

/**
 * 运营状态处理-【调度申请】影响的运营状态
 * @author ql
 * @date 2017年9月16日
 */
@Service
public class ScheduleApplyHandler implements OperationStateHandler{

	private static Logger logger = LoggerFactory.getLogger(BusPositionOperationHandler.class);
	
	@Autowired
	private BusScheduleApplyService busScheduleApplyService;
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionHandlerException {
		MultiLog log = MultiLogUtil.getPositionJudgeLog(busInstance);
		BusScheduleApply applay = busScheduleApplyService.getBusScheduleApply(busInstance.getBusUuid(), busInstance.getTime());
	
		
		String yunying = PropertiesUtils.getString("schedule.apply.yunying");
		if(applay != null){
			
			if(!busScheduleApplyService.isInNotOperateScheduleApplay(applay)){
				return;
			}

			// 非运营状态【包车、加气、维修等】
			busInstance.setScheduApply(PMSUtils.isNull(applay.getBsaType()));
			
		}else{
//			busInstance.setScheduApply(yunying);
		}
		
		if(busInstance.containsAttribute("scheduApply")){
			if(!busInstance.getScheduApply().equals(yunying)){
				logger.debug("\t 调度申请 改变【运营时间状态】【IN_OPER_TIME】 设置为非运营");
				log.debug("\t 调度申请 改变【运营时间状态】【IN_OPER_TIME】 设置为非运营");
				busInstance.setAttributeInOperateTime(Constants.VAL_BUS_OPER_STATUS_0);	// 非运营
			}
		}
	}

}
